home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu250.dms / pu250.adf / Graphics / VSprites / Example1.c < prev    next >
C/C++ Source or Header  |  1992-04-28  |  11KB  |  379 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: VSprites                    Tulevagen 22       */
  8. /* File:    Example1.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-28                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how to get and use a VSprite. The VSprite */
  21. /* can be moved around by the user by pressing the arrow keys.         */
  22.  
  23.  
  24.  
  25. /* Since we use Intuition, include this file: */
  26. #include <intuition/intuition.h>
  27.  
  28. /* Include this file since you are using sprites: */
  29. #include <graphics/gels.h>
  30.  
  31.  
  32.  
  33. /* Declare the functions we are going to use: */
  34. void main();
  35. void clean_up();
  36.  
  37.  
  38.  
  39. struct IntuitionBase *IntuitionBase = NULL;
  40. /* We need to open the Graphics library since we are using sprites: */
  41. struct GfxBase *GfxBase = NULL;
  42.  
  43.  
  44.  
  45. /* Declare a pointer to a Screen structure: */ 
  46. struct Screen *my_screen;
  47.  
  48. /* Declare and initialize your NewScreen structure: */
  49. struct NewScreen my_new_screen=
  50. {
  51.   0,            /* LeftEdge  Should always be 0. */
  52.   0,            /* TopEdge   Top of the display.*/
  53.   640,          /* Width     We are using a high-resolution screen. */
  54.   200,          /* Height    Non-Interlaced NTSC (American) display. */
  55.   2,            /* Depth     4 colours. */
  56.   0,            /* DetailPen Text should be drawn with colour reg. 0 */
  57.   1,            /* BlockPen  Blocks should be drawn with colour reg. 1 */
  58.   HIRES|SPRITES,/* ViewModes High resolution, sprites will be used. */
  59.   CUSTOMSCREEN, /* Type      Your own customized screen. */
  60.   NULL,         /* Font      Default font. */
  61.   "VSprites!",  /* Title     The screen's title. */
  62.   NULL,         /* Gadget    Must for the moment be NULL. */
  63.   NULL          /* BitMap    No special CustomBitMap. */
  64. };
  65.  
  66.  
  67.  
  68. /* Declare a pointer to a Window structure: */ 
  69. struct Window *my_window = NULL;
  70.  
  71. /* Declare and initialize your NewWindow structure: */
  72. struct NewWindow my_new_window=
  73. {
  74.   0,             /* LeftEdge    x position of the window. */
  75.   0,             /* TopEdge     y positio of the window. */
  76.   640,           /* Width       640 pixels wide. */
  77.   200,           /* Height      200 lines high. */
  78.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  79.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  80.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  81.   RAWKEY,        /*             user has selected the Close window gad, */
  82.                  /*             or if the user has pressed a key. */
  83.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  84.   WINDOWCLOSE|   /*             Close Gadget. */
  85.   WINDOWDRAG|    /*             Drag gadget. */
  86.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  87.   WINDOWSIZING|  /*             Sizing Gadget. */
  88.   ACTIVATE,      /*             The window should be Active when opened. */
  89.   NULL,          /* FirstGadget No Custom gadgets. */
  90.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  91.   "Use the arrow keys to move the VSprite!", /* Title */
  92.   NULL,          /* Screen      Will later be connected to a custom scr. */
  93.   NULL,          /* BitMap      No Custom BitMap. */
  94.   80,            /* MinWidth    We will not allow the window to become */
  95.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  96.   640,           /* MaxWidth    than 640 x 200. */
  97.   200,           /* MaxHeight */
  98.   CUSTOMSCREEN   /* Type        Connected to the Workbench Screen. */
  99. };
  100.  
  101.  
  102.  
  103. /* 1. Declare and initialize some sprite */
  104. /*    data for each VSprite:             */
  105. UWORD chip vsprite_data[]=
  106. {
  107.   0x0180, 0x0000,
  108.   0x03C0, 0x0000,
  109.   0x07E0, 0x0000,
  110.   0x0FF0, 0x0000,
  111.   0x1FF8, 0x0000,
  112.   0x3FFC, 0x0000,
  113.   0x7FFE, 0x0000,
  114.   0x0000, 0xFFFF,
  115.   0x0000, 0xFFFF,
  116.   0x7FFE, 0x7FFE,
  117.   0x3FFC, 0x3FFC,
  118.   0x1FF8, 0x1FF8,
  119.   0x0FF0, 0x0FF0,
  120.   0x07E0, 0x07E0,
  121.   0x03C0, 0x03C0,
  122.   0x0180, 0x0180,
  123. };
  124.  
  125.  
  126.  
  127. /* 2. Declare three VSprite structures. One will be used, */
  128. /*    the other two are "dummies":                        */
  129. struct VSprite head, tail, vsprite;
  130.  
  131.  
  132. /* 3. Decide the VSprite's colours:            */
  133. /*                         RGB     RGB     RGB */
  134. WORD colour_table[] = { 0x000F, 0x00F0, 0x0F00 };
  135.  
  136.  
  137. /* 4. Declare a GelsInfo structure: */
  138. struct GelsInfo ginfo;
  139.  
  140.  
  141. /* This boolean variable will tell us if the VSprite is in */
  142. /* the list or not:                                        */
  143. BOOL vsprite_on = FALSE;
  144.  
  145.  
  146. /* This program will not open any console window if run from */
  147. /* Workbench, but we must therefore not print anything.      */
  148. /* Functions like printf() must therefore not be used.       */
  149. void _main()
  150. {
  151.   /* The GelsInfo structure needs the following arrays: */
  152.   WORD nextline[ 8 ];
  153.   WORD *lastcolor[ 8 ];
  154.  
  155.   /* Sprite position: */
  156.   WORD x = 40;
  157.   WORD y = 40;
  158.  
  159.   /* Direction of the sprite: */
  160.   WORD x_direction = 0;
  161.   WORD y_direction = 0;
  162.  
  163.   /* Boolean variable used for the while loop: */
  164.   BOOL close_me = FALSE;
  165.  
  166.   ULONG class; /* IDCMP */
  167.   USHORT code; /* Code */
  168.  
  169.   /* Declare a pointer to an IntuiMessage structure: */
  170.   struct IntuiMessage *my_message;
  171.  
  172.  
  173.  
  174.   /* Open the Intuition Library: */
  175.   IntuitionBase = (struct IntuitionBase *)
  176.     OpenLibrary( "intuition.library", 0 );
  177.   
  178.   if( IntuitionBase == NULL )
  179.     clean_up(); /* Could NOT open the Intuition Library! */
  180.  
  181.  
  182.  
  183.   /* 5. Open the Graphics Library:                                    */
  184.   /* Since we are using sprites we need to open the Graphics Library: */
  185.   /* Open the Graphics Library: */
  186.   GfxBase = (struct GfxBase *)
  187.     OpenLibrary( "graphics.library", 0);
  188.  
  189.   if( GfxBase == NULL )
  190.     clean_up(); /* Could NOT open the Graphics Library! */
  191.  
  192.  
  193.  
  194.   /* We will now try to open the screen: */
  195.   my_screen = (struct Screen *) OpenScreen( &my_new_screen );
  196.  
  197.   /* Have we opened the screen succesfully? */
  198.   if(my_screen == NULL)
  199.     clean_up();
  200.  
  201.  
  202.   my_new_window.Screen = my_screen;
  203.  
  204.  
  205.   /* We will now try to open the window: */
  206.   my_window = (struct Window *) OpenWindow( &my_new_window );
  207.   
  208.   /* Have we opened the window succesfully? */
  209.   if(my_window == NULL)
  210.     clean_up(); /* Could NOT open the Window! */
  211.  
  212.  
  213.  
  214.   /* 6. Initialize the GelsInfo structure: */
  215.  
  216.   /* All sprites except the first two may be used to draw */
  217.   /* the VSprites: ( 11111100 = 0xFC )                    */
  218.   ginfo.sprRsrvd = 0xFC;
  219.   /* If we do not exclude the first two sprites, the mouse */
  220.   /* pointer's colours may be affected.                    */
  221.  
  222.  
  223.   /* Give the GelsInfo structure some memory: */
  224.   ginfo.nextLine = nextline;
  225.   ginfo.lastColor = lastcolor;
  226.  
  227.  
  228.   /* Give the Rastport a pointer to the GelsInfo structure: */
  229.   my_window->RPort->GelsInfo = &ginfo;
  230.  
  231.   
  232.   /* Give the GelsInfo structure to the system: */
  233.   InitGels( &head, &tail, &ginfo );
  234.  
  235.  
  236.  
  237.   /* 7. Initialize the VSprite structure: */
  238.  
  239.   vsprite.Flags = VSPRITE; /* It is a VSprite.            */
  240.   vsprite.X = x;           /* X position.                 */
  241.   vsprite.Y = y;           /* Y position.                 */
  242.   vsprite.Height = 16;     /* 16 lines tall.              */
  243.   vsprite.Width = 2;       /* Two bytes (16 pixels) wide. */
  244.   vsprite.Depth = 2;       /* Two bitplanes, 4 colours.   */
  245.  
  246.  
  247.   /* Pointer to the sprite data: */
  248.   vsprite.ImageData = vsprite_data;
  249.  
  250.   /* Pointer to the colour table: */
  251.   vsprite.SprColors = colour_table;
  252.  
  253.  
  254.  
  255.   /* 8. Add the VSprites to the VSprite list: */
  256.   AddVSprite( &vsprite, my_window->RPort );
  257.  
  258.   /* The VSprite is in the list. */
  259.   vsprite_on = TRUE;
  260.   
  261.  
  262.  
  263.   /* Stay in the while loop until the user has selected the Close window */
  264.   /* gadget: */
  265.   while( close_me == FALSE )
  266.   {
  267.     /* Stay in the while loop as long as we can collect messages */
  268.     /* sucessfully: */
  269.     while(my_message = (struct IntuiMessage *) GetMsg(my_window->UserPort))
  270.     {
  271.       /* After we have collected the message we can read it, and save any */
  272.       /* important values which we maybe want to check later: */
  273.       class = my_message->Class;
  274.       code  = my_message->Code;
  275.  
  276.  
  277.       /* After we have read it we reply as fast as possible: */
  278.       /* REMEMBER! Do never try to read a message after you have replied! */
  279.       /* Some other process has maybe changed it. */
  280.       ReplyMsg( my_message );
  281.  
  282.  
  283.       /* Check which IDCMP flag was sent: */
  284.       switch( class )
  285.       {
  286.         case CLOSEWINDOW:     /* Quit! */
  287.                close_me=TRUE;
  288.                break;  
  289.  
  290.         case RAWKEY:          /* A key was pressed! */
  291.                /* Check which key was pressed: */
  292.                switch( code )
  293.                {
  294.                  /* Up Arrow: */
  295.                  case 0x4C:      y_direction = -1; break; /* Pressed */
  296.                  case 0x4C+0x80: y_direction = 0;  break; /* Released */
  297.  
  298.                  /* Down Arrow: */
  299.                  case 0x4D:      y_direction = 1; break; /* Pressed */
  300.                  case 0x4D+0x80: y_direction = 0; break; /* Released */
  301.  
  302.                  /* Right Arrow: */
  303.                  case 0x4E:      x_direction = 2; break; /* Pressed */
  304.                  case 0x4E+0x80: x_direction = 0; break; /* Released */
  305.  
  306.                  /* Left Arrow: */
  307.                  case 0x4F:      x_direction = -2; break; /* Pressed */
  308.                  case 0x4F+0x80: x_direction = 0;  break; /* Released */
  309.                }
  310.                break;  
  311.       }
  312.     }
  313.  
  314.  
  315.  
  316.     /* 12. Play around with the VSprite: */
  317.  
  318.     /* Change the x/y position: */
  319.     x += x_direction;
  320.     y += y_direction;
  321.  
  322.     /* Check that the sprite does not move outside the screen: */
  323.     if(x > 640)
  324.       x = 640;
  325.     if(x < 0)
  326.       x = 0;
  327.     if(y > 200)
  328.       y = 200;
  329.     if(y < 0)
  330.       y = 0;
  331.  
  332.     vsprite.X = x;
  333.     vsprite.Y = y;
  334.  
  335.  
  336.  
  337.     /* 9. Sort the Gels list: */
  338.     SortGList( my_window->RPort );
  339.  
  340.     /* 10. Draw the Gels list: */
  341.     DrawGList( my_window->RPort, &(my_screen->ViewPort) );
  342.  
  343.     /* 11. Set the Copper and redraw the display: */
  344.     MakeScreen( my_screen );
  345.     RethinkDisplay();    
  346.   }
  347.  
  348.  
  349.  
  350.   /* Free all allocated memory: (Close the window, libraries etc) */
  351.   clean_up();
  352.  
  353.   /* THE END */
  354. }
  355.  
  356.  
  357.  
  358. /* This function frees all allocated memory. */
  359. void clean_up()
  360. {
  361.   /* 13. Remove the VSprites: */
  362.   if( vsprite_on )
  363.     RemVSprite( &vsprite );
  364.  
  365.   if( my_window )
  366.     CloseWindow( my_window );
  367.   
  368.   if(my_screen )
  369.     CloseScreen( my_screen );
  370.  
  371.   if( GfxBase )
  372.     CloseLibrary( GfxBase );
  373.  
  374.   if( IntuitionBase )
  375.     CloseLibrary( IntuitionBase );
  376.  
  377.   exit();
  378. }
  379.